<?php
/**
 * 主界面控制器
 *
 * @author 窦子滨 <zibin_5257@163.com>
 */
class MainAction extends Action{
    function _initialize()  //控制器初始化方法，进行权限的认证
    {
         if(!isset($_SESSION['user_name']) or $_SESSION['user_name']==''){
            pop('请正确登录系统！',U('index/index'));
         }
    }
    /**
     * 主体框架 顶部菜单条控制器
     */
    function top_menu(){
        $Cache=D('Cache');
        $user_info=$Cache->get_userinfo($_SESSION['user_name']);
        $this->assign($user_info);
        $this->display();
    }
    /**
     * 主体框架，左侧导航条控制器
     */
    function left(){
        //系统公共方法库
        $this->display();
    }

    /**
     * 右侧排行榜控制器
     */
    public function right(){
        //建立一个执行SQL的数据模型
        $SQL = M();
        
        $now_date=get_year_month(time());
        
        //dump($now_date);

        //校区新签人数排行榜开始
        //$sql = "select count(t1.stu_id) as num,bm_name as name from lfy_stu_hetong as t1 left join (lfy_stu_xiaoqu as t2,lfy_bumen as t3) on (t1.stu_id=t2.stu_id and t3.bm_id=t2.xq_id) where left(ht_input_time,7)='2011-04' group by t2.xq_id order by num desc limit 3";
        $sql = "select "
            ."(select count(t2.stu_id) "
            ."from lfy_stu_hetong as t2,lfy_stu_xiaoqu as t3 "
            ."where t2.stu_id=t3.stu_id and t3.xq_id=t1.bm_id and left(t2.ht_input_time,7)='{$now_date}') as num,t1.bm_name as name "
        ."from lfy_bumen as t1 where t1.bm_kind='s' order by num desc,t1.bm_name limit 3";
        $list = $SQL->query($sql);
        
        //dump($list);
        $this->assign('xiaoquxinzhao_num', $list);
        unset ($list);
        //校区新签人数排行榜结束
        
        
        //校区本月新签额排行榜开始
        $sql = "select ifnull("
        ."(select sum(t2.hetong_price) from "
            ."lfy_stu_hetong as t2,lfy_stu_xiaoqu as t3 "
            ."where t2.stu_id=t3.stu_id and t3.xq_id=t1.bm_id and left(t2.ht_input_time,7)='{$now_date}'),0) "
            ."as num,t1.bm_name as name "
        ."from lfy_bumen as t1 where t1.bm_kind='s' order by num desc,t1.bm_name";
        $list = $SQL->query($sql);
        //dump($list);
        $this->assign('campus_Signed_list', $list);
        unset ($list);
        //校区本月新签额排行榜结束
        
        
        //校区本月新签实收排行榜开始
        $sql = "select ifnull("
        ."(select sum(t2.sk_price) "
            ."from lfy_qt_htshoukuan as t2,lfy_stu_xiaoqu as t3 "
            ."where t2.stu_id=t3.stu_id and t3.xq_id=t1.bm_id and left(t2.sk_input_time,7)='{$now_date}'),0) as num,t1.bm_name as name "
        ."from lfy_bumen as t1 where t1.bm_kind='s' order by num desc,t1.bm_name";
        $list = $SQL->query($sql);
        //dump($list);
        $this->assign('campus_paid_list', $list);
        unset ($list);


        //教师排行榜数据
        $sql="select t2.user_zsname,t2.user_id,t2.bm_id,"
        ."ifnull("
            ."(select sum(jf_num) "
                ."from lfy_teacher_jifen as t3 "
                ."where t3.user_id=t1.user_id),0) "
            ."as jifen "
        ."from lfy_user_shenfen as t1 "
        ."left join lfy_user as t2 "
        ."on t2.user_id=t1.user_id "
        ."where t1.sf_kind='t' "
        ."order by jifen desc limit 10";
        $list=$SQL->query($sql);
        unset($SQL);
        
        //dump($list);
        $count=count($list);

        //教师等级信息模块
        $Level=M('Teacher_level');

        //dump($list);
        
        for($i=0;$i<$count;$i++){
            $info=$Level->where("tl_num<={$list[$i]['jifen']}")->order('tl_num desc')->find();
            $list[$i]['level']=$info['tl_no'];
            unset($info);
            $next_no=$list[$i]['level']+1;
            //dump($next_no);
            $next_info=$Level->where("tl_no={$next_no}")->find();

            $list[$i]['next_level']=$next_info['tl_num'];
        }
        //dump($list);
        unset($Level);

        $this->assign('teacher_level_list', $list);
        unset($list);

        $this->display();
    }
    
    
    /**
     * 界面获取在线人数数据 并删除历史的无用记录
     */
    Public function get_online_num(){
        //获取当前时间
        $time=time();
        $time_top=$time-10;
        $Online=M('Online_user');
        
        if($Online->where("user_id={$_SESSION['user_id']}")->count()>0){
            $Online->where("user_id={$_SESSION['user_id']}")->setField('now_time',$time);
        }else{
            $data=array(
                'user_id'=>$_SESSION['user_id'],
                'now_time'=>$time
            );
            $Online->add($data);
            unset($data);
        }
        
        $Online->where("now_time<$time_top")->delete();
        
        $count=$Online->where("now_time>={$time_top}")->count();
        unset($Online);
        $this->ajaxReturn($count);
    }


    /**
     * 显示当前在线用户列表
     */
    public function all_online_user(){
        $Online_user=M('Online_user');
        $list=$Online_user->join("lfy_user on lfy_user.user_id=lfy_online_user.user_id")->select();
        //dump($list);
        $this->assign('list', $list);
        unset($list);
        unset($Online_user);
        $this->display();
    }


}
?>
